import flash.display.Sprite;
import flash.display.Shape;
import flash.display.InteractiveObject;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;

/*

  The mouse events here do not work yet in NME - this is a test case.

*/

class Events extends Sprite
{

   var mCircle:Shape;
   var mLineShape:Shape;
   var mSpriteChild:Sprite;
   var mText:TextField;
   
   public function new()
   {
      super();
      flash.Lib.current.addChild(this);

      // creating a new shape instance
      mCircle = new Shape( ); 
      mCircle.graphics.beginFill( 0xff9933 , 1 );
      mCircle.graphics.drawCircle( 0 , 0 , 40 );
      addChild( mCircle ); 

      // creating a new shape instance
      mLineShape = new Shape( ); 
      var gfx = mLineShape.graphics;
      gfx.lineStyle( 20, 0x80ff80 );
      gfx.moveTo( -200,0 );
      gfx.curveTo( 0, 400, 200,0 );
      addChild( mLineShape ); 


      // A child sprite, which is an Interactive object
      mSpriteChild = new Sprite( ); 
      var gfx2 = mSpriteChild.graphics;
      gfx2.lineStyle( 20, 0x33ff33 );
      gfx2.moveTo( -200,0 );
      gfx2.curveTo( 0, 200, 200,0 );
      AddMouseFuncs(mSpriteChild,"Sprite");
      addChild( mSpriteChild ); 

      mText = new TextField();
      mText.htmlText = "Hello!";
      mText.x = -200;
      mText.y = -200;
      addChild(mText);
      AddMouseFuncs(mText,"Text");

      // Not sure how transparent image should go ...
      var loader = new flash.display.Loader();
      loader.load(new URLRequest("test.png"));

      loader.x = -50;
      loader.y = -200;
      addChild(loader);
      AddMouseFuncs(loader,"Loader(bitmap)");


      x=320;
      y=240;

      addEventListener(MouseEvent.ROLL_OVER,OnOver);
      addEventListener(MouseEvent.ROLL_OUT,OnOut);
      addEventListener(MouseEvent.CLICK,OnClick);

      AddMouseFuncs(this,"Base");
   }

   function OnOver(event:Event) : Void
   {
      trace("OnOver");
      mCircle.graphics.clear();
      mCircle.graphics.beginFill( 0xff0000 , 1 );
      mCircle.graphics.drawCircle( 0 , 0 , 40 );
   }

   function OnOut(event:Event) : Void
   {
      trace("OnOut");
      mCircle.graphics.clear();
      mCircle.graphics.beginFill( 0xff9933 , 1 );
      mCircle.graphics.drawCircle( 0 , 0 , 40 );
   }

   function OnClick(event:Event) : Void
   {
      trace("CLICK!");
   }


   function AddMouseFuncs(inObj:InteractiveObject,inName:String)
   {
      inObj.addEventListener(MouseEvent.MOUSE_DOWN, function(inEvt:Event)
         {  trace("MouseDown : " + inName); } );
      inObj.addEventListener(MouseEvent.MOUSE_UP, function(inEvt:Event)
         {  trace("MouseUp : " + inName); } );
   }


   static public function main()
   {
      neash.Lib.Init("Events",640,480,false,true); 
      neash.Lib.SetBackgroundColour(0xffffff); 

      new Events();

      // neash addition ...
      neash.Lib.Run(); 
   }
}

